package com.bana.fvs.service;

import com.bana.fvs.code.*;
import com.bana.fvs.dao.VisibilityScaleDAO;
import com.bana.fvs.model.VisibilityScale;
import com.bana.sys.service.CodeService;
import com.bana.sys.utils.Tuple;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Service
public class VisibilityScaleService {

    @Resource
    private VisibilityScaleDAO visibilityScaleDAO;

    @Resource
    private ControllerService controllerService;

    public  List<VisibilityScale> getVisibilityScales(int wqbm)
    {
        List<VisibilityScale> list=visibilityScaleDAO.getVisibilityScales(wqbm);
        if(list.isEmpty())
        {
            list= visibilityScaleDAO.getVisibilityScales(0);
        }
        return list;
    }

    public VisibilityScale getVisibilityScale(int wqbm,int njd)
    {
        List<VisibilityScale> list=getVisibilityScales(wqbm);
        for(int i = 0; i< 10; ++i)
        {
            VisibilityScale visibilityScale=list.get(i);
            if(visibilityScale!=null)
            {
                if(njd>visibilityScale.getNjdsx())
                {
                    if(i==0)
                    {
                        VisibilityScale result= list.get(0);
                        result.setNjdxx(result.getNjdsx());
                        return result;
                    }
                    else
                    {
                        VisibilityScale result= list.get(i-1);
                        result.setNjdxx(list.get(i).getNjdsx());
                        return result;
                    }

                }
                else if(njd== visibilityScale.getNjdsx())
                {
                    if(i==0)
                    {
                        VisibilityScale result= list.get(0);
                        result.setNjdxx(result.getNjdsx());
                        return result;
                    }
                    else
                    {
                        VisibilityScale result= list.get(i-1);
                        result.setNjdxx(list.get(i).getNjdsx());
                        return result;
                    }
                }
            }
        }
        VisibilityScale result= list.get(9);
        result.setNjdxx(0);
        return result;
    }

    public void updateVisibilityScale(VisibilityScale visibilityScale)
    {
        //计算策略的数值
        if(visibilityScale.getKzcl()>= ControlStrategy.Custom)
        {
            visibilityScale.setWdld(visibilityScale.getWdld());
            visibilityScale.setLdsc(visibilityScale.getLdsc());
            visibilityScale.setMdsc(visibilityScale.getMdsc());
        }
        else {
            visibilityScale.setWdld(controllerService.getWdld(visibilityScale.getWdlddj()));

            Tuple<Integer,Integer> t=controllerService.getLdsc(visibilityScale.getSspl(),visibilityScale.getZkb());
            visibilityScale.setLdsc(t.getItem1());
            visibilityScale.setMdsc(t.getItem2());
        }
        if(visibilityScale.getWdkz()==ControllerWorkMode.Off)
        {
            visibilityScale.setWdld(1);
            visibilityScale.setLdsc(0);
            visibilityScale.setMdsc(1);
        }
        VisibilityScale v=visibilityScaleDAO.getVisibilityScale(visibilityScale.getWqbm(),visibilityScale.getNjddj());
        if(v==null)
        {
            visibilityScaleDAO.insertVisibilityScale(visibilityScale);
        }
        else
        {
            visibilityScaleDAO.updateVisibilityScale(visibilityScale);
        }
    }

    public int getWarningNjd(int wqbm)
    {
        List<VisibilityScale> list=getVisibilityScales(wqbm);
        for(int i=0;i<10;++i)
        {
            if(list.get(i).getWdkz()==ControllerWorkMode.FlashRed||
                    list.get(i).getWdkz()==ControllerWorkMode.FlashYellow||
                    list.get(i).getWdkz()==ControllerWorkMode.Detection)
            {
                return list.get(i).getNjdsx();
            }
        }
        return 0;
    }
}
